//
//  XGLoginViewController.m
//  Instagram
//
//  Created by monkey on 2019/4/13.
//  Copyright © 2019 itcast. All rights reserved.
//

#import <Masonry/Masonry.h>
#import <SVProgressHUD/SVProgressHUD.h>

#import "XGLoginViewController.h"
#import "XGRegisterViewController.h"
#import "XGResetPasswordViewController.h"
#import "XGVerifyEmailViewController.h"

@interface XGLoginViewController ()

/// 标题
@property (nonatomic,strong) UILabel *titleLabel;
/// 邮箱
@property (nonatomic,strong) UITextField *emailTextField;
/// 密码
@property (nonatomic,strong) UITextField *passwordTextField;
/// 忘记密码
@property (nonatomic,strong) UIButton *forgetPasswordButton;
/// 验证邮箱
@property (nonatomic,strong) UIButton *verifyEmailButton;
/// 登录按钮
@property (nonatomic,strong) UIButton *loginButton;
/// 注册按钮
@property (nonatomic,strong) UIButton *registerButton;

@end

@implementation XGLoginViewController

#pragma mark - 控制器生命周期方法

- (void)loadView
{
    [super loadView];
    
    [self setUpUI];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchViewForHiddenKeyboardAction)];
    [self.view addGestureRecognizer:tap];
}

#pragma mark - 事件监听

/// 隐藏键盘通知监听事件
- (void)touchViewForHiddenKeyboardAction
{
    [self.view endEditing:YES];
}

/// 登录监听事件
- (void)loginAction
{
    // 退出键盘
    [self.view endEditing:YES];
    
    // 判断字段为空
    if (!_emailTextField.hasText || !_passwordTextField.hasText) {
        [SVProgressHUD showErrorWithStatus:@"用户名或密码为空!"];
        return;
    }
    
    // 验证邮箱格式
    if (!_emailTextField.text.isEmail) {
        [SVProgressHUD showErrorWithStatus:@"邮箱格式不正确!请检查后重新填写"];
        return;
    }
    
    // 处理登录
    [XGAccountViewModel loginWithEmail:_emailTextField.text password:_passwordTextField.text completion:^(BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            [SVProgressHUD showSuccessWithStatus:message];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                // 发布通知 切换应用程序根控制器
                [[NSNotificationCenter defaultCenter] postNotificationName:kSwitchApplicationRootViewControllerNotification object:nil];
            });
        } else {
            [SVProgressHUD showErrorWithStatus:message];
            return;
        }
    }];
}

/// 注册监听事件
- (void)registerAction
{
    XGRegisterViewController *registerViewController = [[XGRegisterViewController alloc] init];
    [self presentViewController:registerViewController animated:YES completion:nil];
}

/// 重置密码监听事件
- (void)resetPasswordAction
{
    XGResetPasswordViewController *viewController = [[XGResetPasswordViewController alloc] init];
    [self presentViewController:viewController animated:YES completion:nil];
}

/// 验证邮箱监听事件
- (void)verifyEmailAction
{
    XGVerifyEmailViewController *viewController = [[XGVerifyEmailViewController alloc] init];
    [self presentViewController:viewController animated:YES completion:nil];
}

#pragma mark - 代理

#pragma mark - 界面布局

/// 设置界面
- (void)setUpUI
{
    self.view.backgroundColor = [UIColor red:210 green:254 blue:91 alpha:1];
    
    // 添加子控件
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.emailTextField];
    [self.view addSubview:self.passwordTextField];
    [self.view addSubview:self.forgetPasswordButton];
    [self.view addSubview:self.verifyEmailButton];
    [self.view addSubview:self.loginButton];
    [self.view addSubview:self.registerButton];
    
    // 设置自动布局
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(100);
    }];
    
    [_emailTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(30);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
    
    [_passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.emailTextField.mas_bottom).offset(15);
        make.left.right.equalTo(self.emailTextField);
    }];
    
    [_forgetPasswordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordTextField.mas_bottom).offset(5);
        make.left.equalTo(self.passwordTextField);
    }];
    
    [_verifyEmailButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.forgetPasswordButton);
        make.right.equalTo(self.passwordTextField);
    }];
    
    [_loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.forgetPasswordButton.mas_bottom).offset(15);
        make.left.equalTo(self.forgetPasswordButton);
        make.size.mas_equalTo(CGSizeMake(80, 36));
    }];
    
    [_registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.passwordTextField);
        make.top.equalTo(self.loginButton);
        make.size.equalTo(self.loginButton);
    }];
}

#pragma mark - 懒加载

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithText:@"Instagram" textColor:[UIColor darkGrayColor] font:30 textAlignment:NSTextAlignmentCenter];
    }
    
    return _titleLabel;
}

- (UITextField *)emailTextField
{
    if (!_emailTextField) {
        _emailTextField = [UITextField textFieldWithPlaceholder:@"邮箱地址" fontSize:15 textColor:[UIColor darkTextColor] isSecureTextEntry:NO keyboardType:UIKeyboardTypeEmailAddress];
    }
    
    return _emailTextField;
}

- (UITextField *)passwordTextField
{
    if (!_passwordTextField) {
        _passwordTextField = [UITextField textFieldWithPlaceholder:@"密码" fontSize:15 textColor:[UIColor darkTextColor] isSecureTextEntry:YES keyboardType:UIKeyboardTypeDefault];
    }
    
    return _passwordTextField;
}

- (UIButton *)forgetPasswordButton
{
    if (!_forgetPasswordButton) {
        _forgetPasswordButton = [UIButton buttonWithTitle:@"忘记密码" fontSize:15 titleColor:[UIColor darkGrayColor] target:self action:@selector(resetPasswordAction)];
    }
    
    return _forgetPasswordButton;
}

- (UIButton *)verifyEmailButton
{
    if (!_verifyEmailButton) {
        _verifyEmailButton = [UIButton buttonWithTitle:@"验证邮箱" fontSize:15 titleColor:[UIColor darkGrayColor] target:self action:@selector(verifyEmailAction)];
    }
    
    return _verifyEmailButton;
}

- (UIButton *)loginButton
{
    if (!_loginButton) {
        _loginButton = [UIButton buttonWithTitle:@"登录" fontSize:15 titleColor:[UIColor whiteColor] target:self action:@selector(loginAction)];
        _loginButton.backgroundColor = [UIColor red:245 green:90 blue:93 alpha:1];
    }
    
    return _loginButton;
}

- (UIButton *)registerButton
{
    if (!_registerButton) {
        _registerButton = [UIButton buttonWithTitle:@"注册" fontSize:15 titleColor:[UIColor whiteColor] target:self action:@selector(registerAction)];
        _registerButton.backgroundColor = [UIColor orangeColor];
    }
    
    return _registerButton;
}

@end
